#include "Reader.h"


Reader::Reader(std::string datinp){
    fl1 = datinp;
    int n = datinp.length()-7;
    fl2 = datinp.substr(0,n)+".log";
}

int Reader::LineCounter(){
    int numlines = 0;
    ifstream cl;
    string s;
    char filein[1000];
    strcpy(filein, fl1.c_str());
    cl.open (filein);
    if (cl.is_open()){
    while (getline(cl, s)){
        numlines++;
    }
    cl.close();
    numlines= numlines-4;
    return numlines;

    }
    else{
        cout << "Could Not Open File" << endl;
        exit(0);
        return 0;
    }


}

vector<vector<long double> > Reader::LibelaDat(){


        int nlines = this->LineCounter();

        char filein[1000];
        //transforma string em char
        strcpy(filein, fl1.c_str());

        long double step, energy, rmsd, DX, DY, DZ, alpha, beta, gamma, nconf, confenergy;
        vector< vector<long double> > ner;
        vector<long double> tner;

        FILE * fille;
        char buffer[10000];
        fille = fopen (filein,"r");
        for (int i=0; i<nlines; i++){
            tner.clear();
            if (i==0)
            {
            fgets(buffer,1000, fille);
            fscanf(fille, "BoxsideX=    %Lf BoxsideY=    %Lf BoxsideY=    %Lf Temperature=   %Lf ", &boxx, &boxy, &boxz, &temp);
            fgets(buffer,1000, fille);
            fgets(buffer,1000, fille);

            }


            else{
                fscanf(fille, "%Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf", &step, &energy, &rmsd, &DX, &DY, &DZ, &alpha, &beta, &gamma, &nconf, &confenergy);
                tner.push_back (step);
                tner.push_back (energy);
                tner.push_back (rmsd);
                tner.push_back (DX);
                tner.push_back (DY);
                tner.push_back (DZ);
                tner.push_back (alpha);
                tner.push_back (beta);
                tner.push_back (gamma);
                tner.push_back (nconf);
                tner.push_back (confenergy);
                ner.push_back (tner);

                }
            }



        fclose(fille);
        cout<<"File successfully Read "<< endl;
        return ner;
}


void Reader::Logreader(){

    ifstream cl;
    string s = "";
    char filein[1000];
    strcpy(filein, fl2.c_str());
    cl.open(filein);

    while(s !="* generate_conformers            1                                                                 *"){
        getline(cl,s);
    }
    cl >> s >> s >> this->nconf;

    while(s !="Conformer energies:"){
        getline(cl,s);
    }

    int n;
    long double eneconf;

    for(int i=0; i<this->nconf; i++){
        getline(cl,s);
        if (s == "****************************************************************************************************"){
                            this->Rnconf = i+1;
                            break;
            }
        cl >> n >> eneconf;
        this->enerconf.push_back(eneconf);
        getline(cl,s);



    }

    while (s != "Max Dimensions:" ){

                getline(cl,s);
            }

    cl >> this->volx >> this->voly >> this->volz;



}






